package com.uidp.domain;

import java.util.List;
import static com.uidp.bundle.Messages.ACTIVE_USER_NOT_FOUND;
import com.acsior.dac.exeption.DataAccessException;
import com.acsior.sbc.exception.BusinessException;
import com.acsior.sbc.exception.SearchException;
import com.acsior.sbc.exception.ValidationException;
import com.acsior.sbc.impl.hibernate.HibernateBusinessObject;
import com.uidp.hibernatelayer.UsersDAO;
import com.uidp.hibernatelayer.UsersVO;
import com.uidp.search.UserSearch;
import com.uidp.security.Digester;
import com.uidp.security.IUser;

public class Users extends HibernateBusinessObject<UsersVO, UsersDAO> implements
		IUser {

	/**
	 * Търсене на активен потребител по потребителско име
	 * 
	 * @param username
	 *            - потребителско име
	 * @return IUser - резултат
	 * 
	 * @throws BusinessException
	 * @throws SearchException
	 */
	public static IUser searchActiveByUserName(String username)
			throws BusinessException, SearchException {
		UserSearch search = new UserSearch(username);
		search.execute();

		if (search.getResult().isEmpty()) {
			throw new BusinessException(ACTIVE_USER_NOT_FOUND, username);
		}

		return search.getResult().get(0);
	}

	/**
	 * Търсене на активен потребител по потребителско име
	 * 
	 * @param username
	 *            - потребителско име
	 * @return IUser - резултат
	 * 
	 * @throws BusinessException
	 * @throws SearchException
	 */
	public static List<Users> searchAllActiveByUserName(String username,
			String firstName, String family, String egn, Usergroups us)
			throws BusinessException, SearchException {
		UserSearch search = new UserSearch(username, firstName, family, egn, us);
		search.execute();
		return search.getResult();
	}

	private Usergroups usergroups;
	private String username;
	private String password;
	private String firstName;
	private String surname;
	private String egn;
	private String family;
	private String email;

	/** списък с позволени ресурси за потребителя */
	private List<String> resources;

	/**
	 * @param str
	 * @return encoded string
	 */
	public static String ENCRYPT_PASSWORD(String str) {
		return Digester.digestSHA1(str);
	}

	/**
	 * Смяна на парола
	 * 
	 * @param newPassword
	 * @throws BusinessException
	 */
	public void changePassword(String newPassword) throws BusinessException {
		setPassword(ENCRYPT_PASSWORD(newPassword));
		store();
	}

	/** getAllowedResources() */
	public List<String> getAllowedResources() {
		return this.resources;
	}

	/**
	 * isPasswordSame(java.lang.String)
	 */
	public boolean isPasswordSame(String pwd) {
		return pwd != null && ENCRYPT_PASSWORD(pwd).equals(getPassword());
	}

	public Users() throws BusinessException {
		super();
	}

	/**
	 * @param e
	 * @throws BusinessException
	 */
	public Users(UsersVO h) throws BusinessException {
		super(h);
	}

	/** @see PersistentObject#initialize() */
	@Override
	protected void initialize() throws BusinessException {
		super.initialize();
		this.setUsergroups(new Usergroups());

	}

	@Override
	protected void setValues(UsersVO h) throws DataAccessException {
		h.setUsername(getUsername());
		h.setPass(getPassword());
		h.setFirstName(getFirstName());
		h.setSurname(getSurname());
		h.setFamily(getFamily());
		h.setEgn(getEgn());
		h.setEmail(getEmail());
		h.setUsergroups(getUsergroups().findData());

	}

	@Override
	protected void copyProperties(UsersVO d, boolean deepCopy)
			throws BusinessException {
		setUsername(d.getUsername());
		setPassword(d.getPass());
		setFirstName(d.getFirstName());
		setSurname(d.getSurname());
		setFamily(d.getFamily());
		setEgn(d.getEgn());
		setEmail(d.getEmail());
		setUsergroups(new Usergroups(d.getUsergroups()));

	}

	@Override
	protected void validate() throws ValidationException {
		// TODO Auto-generated method stub

	}

	@Override
	protected Class<UsersDAO> getDAOClass() {
		return UsersDAO.class;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getSurname() {
		return surname;
	}

	public void setSurname(String surname) {
		this.surname = surname;
	}

	public String getEgn() {
		return egn;
	}

	public void setEgn(String egn) {
		this.egn = egn;
	}

	public String getFamily() {
		return family;
	}

	public void setFamily(String family) {
		this.family = family;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	/**
	 * @return the usergroups
	 */
	public Usergroups getUsergroups() {
		return usergroups;
	}

	/**
	 * @param usergroups
	 *            the usergroups to set
	 */
	public void setUsergroups(Usergroups usergroups) {
		this.usergroups = usergroups;
	}

	public List<String> getResources() {
		return resources;
	}

	public void setResources(List<String> resources) {
		this.resources = resources;
	}

	public String getName() {
		// TODO Auto-generated method stub
		return null;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public Boolean isActive() {
		// TODO Auto-generated method stub
		return null;
	}

}
